<?php 
session_start();
if (!isset($_SESSION['idUsr'])){
	die("Se necesita iniciar sesión para acceder a esta página");
}
error_log( print_r($_GET,1),3,"/tmp/log");
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="Javi Basauri">
<link rel="shortcut icon" href="../assets/ico/favicon.png">


<title>Cadena de favores</title>

<!-- Bootstrap core CSS -->
<link href="../dist/css/bootstrap.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="../css/offcanvas.css" rel="stylesheet">
<link href="../css/datepicker.css" rel="stylesheet">

<!-- My CSS -->

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
      <script src="assets/js/html5shiv.js"></script>
      <script src="assets/js/respond.min.js"></script>
    <![endif]-->
</head>

<body>

	<div class="navbar navbar-inverse navbar-fixed-top">
		<div class="container">
			<div class="navbar-header">
				<button type="button" class="navbar-toggle" data-toggle="collapse"
					data-target=".navbar-collapse">
					<span class="icon-bar"></span> <span class="icon-bar"></span> <span
						class="icon-bar"></span>
				</button>
				<a class="navbar-brand" href="./home.php">Cadena de Favores</a>
			</div>
			<div class="collapse navbar-collapse pull-left">
				<ul class="nav navbar-nav">
					<li class="active" id="home"><a href="home.php">Home</a></li>
					<li class="dropdown" id="favLi"><a href="#" class="dropdown-toggle"
						data-toggle="dropdown">Favores <b class="caret"></b>
					</a>
						<ul class="dropdown-menu">
							<li><a data-toggle="modal" href="#favModal"
								onclick="refreshfavModal();">Crear favor</a></li>
							<li><a href="#favoresPunt" id="puntFavour">Favores por puntuar <span
									id="userFavCount" class="badge"></span>
							</a></li>
							<li><a href="#misFavores" id="myFavour">Mis favores</a></li>

						</ul>
					</li>
				</ul>
			</div>
			<div class="collapse navbar-collapse pull-right">
				<ul class="nav navbar-nav">
					<li><?php 
					include_once ("DBKonexioa.php");
					$connection= new DBKonexioa();
					if ($connection->konektatu()!=true){
						die("Error al conectar a la base de datos");
					}else{

						$id= $_SESSION['idUsr'];
						$sql="SELECT * FROM usuario WHERE id_usuario=$id";
						$res=mysql_query($sql);
						$row = mysql_fetch_assoc($res);

						if ($row['tipo']==2){
							$style="style=\"display:none;\"";

						}else{
							$style="";
						}
						echo "<li class=\"dropdown\" $style><a href=\"#administrar\" class=\"dropdown-toggle\"
						data-toggle=\"dropdown\">Herramientas de administrador<b class=\"caret\"></b></a>";
						echo "<ul class=\"dropdown-menu\">";
						//badge implementatuko dut, bertan zenbat emaitza dauden azalduko ditut.
						echo "<li><a id=\"listaDenuncias\" href=\"#listaDenuncias\"> Favores Denunciados <span id=\"countDen\" class=\"badge\"></span></a></li>";
						echo "<li><a data-toggle=\"modal\" href=\"#addCategoriaModal\" > Añadir una categoría</a></li>";
						echo "</ul>";
						echo "</li>";


						echo "<li id=\"usrLi\"><a href=\"#usuario\"> $row[nombre] $row[apellido]</a></li>";

					}
					$connection->konexioaItxi();
					?>
					
					<li><a href="logOut.php">Salir</a>
					</li>
				</ul>
			</div>
			<!--/.nav-collapse -->
		</div>
	</div>

	<div class="container">
		<div id="rowCanvas" class="row row-offcanvas row-offcanvas-right">
			<?php if(isset($_GET['changed']) && $_GET['changed']>='1'){
				$changed=$_GET['changed'];
				include("editFav.php?idFav=".$changed);
			}else{
					
					

				echo"<div class=\"col-xs-12 col-sm-9 well\" id=\"main\">";
				echo "<div id=\"mainContainer\" class=\"container\">";


				if(isset($_GET['add']) && $_GET['add']=='1'){


					include_once("DBKonexioa.php");

					$konexioa = new DBKonexioa();

					if ($konexioa->konektatu()!=true){
						die ("No se ha podido conectar a la base de datos");
					}else{


						$id = $_SESSION['idUsr'];
						$sql = "SELECT * FROM favor WHERE fk_usuario='$id'";
						$res = mysql_query($sql);
						if (mysql_num_rows($res)>0){

							while ($row = mysql_fetch_assoc($res)){
								echo "<div id=\"my$row[idfavor]\"class=\"row panel panel-info\">";
								echo "<div class=\"panel-heading\"><h3>$row[nombre]</h3></div>";

								echo "<div class=\"panel-body\">";
								echo "<div class=\"col-md-4\">";

								if($row['foto']==""){
									$foto="image_not_found.jpg";
								}else{
									$foto=$row['foto'];
								}

								echo "<img class=\"img-thumbnail\" src=\"../images/favImages/$foto\">";

								echo "</div>";//cierra div de foto
								if($row['puntuacion']==0){
									$puntuacion="Todavía no puntuado";
								}else{
									$puntuacion=$row['puntuacion']/$row['contador'];
								}
								echo "<div class=\"col-md-4\"><br><h2>Puntuación: $puntuacion</h2><br>
								<p> Tipo de favor: $row[tipo]</p></div>";//cierra div de puntuacion
								echo "<div id=\"boton-fav\" class=\"col-md-4\"><br><br><br>";
								echo "<input  class=\"btn btn-info btn-lg\" type=\"button\" value=\"Editar favor\" onclick=\"showFavour($row[idfavor]);\">";
								echo "    ";
								echo "<button id=\"erase\" onclick=\"deleteFavour($row[idfavor])\" class=\"btn btn-danger btn-lg erase\">Eliminar</button>";

								echo "</div>";//cierra div de botones

								echo "</div>";//cierra div de panel body

								echo "</div>";//cierra div de panelrow





							}
						}
						$konexioa->konexioaItxi();
					}

				}else{

					include("main.php");

				}
				echo "</div>";

				echo"</div>";
				//div de los favores
				echo "<div class=\"col-xs-6 col-sm-3 sidebar-offcanvas\" id=\"sidebar\"
				role=\"navigation\">";
				echo "<div class=\"well sidebar-nav\">";
				include("buscador.php");

				echo"</div>";
				//well
				echo "</div>";
				//span
				echo "</div>";

}?>

		</div>
	</div>

	<!-- Modal -->
	<div class="modal fade" id="favModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title">Nuevo Favor</h4>
				</div>
				<div class="modal-body">
					<?php include("newFavour.php")?>
				</div>
				<div id="favFooter" class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
					<button onclick="createFav();" type="button"
						class="btn btn-primary" id="createFavBut">Crear Favor</button>
				</div>
				<div id="photoFooter" class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal"
						onclick="showMyFavours();">No quiero añadir foto</button>
					<button id="photoButton" type="button" class="btn btn-primary"
						onclick="addPhoto();">Añadir foto</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->
	<!-- Modal -->

	<!-- Modal para añadir la categoria -->

	<div class="modal fade" id="addCategoriaModal" tabindex="-1"
		role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">Añade una nueva categoría</h4>
				</div>
				<div class="modal-body">
					<div class="container">
						<div id="divCat" class="row">
							<form id="formAddCat">
								<div class="form-group">
									<label for="newCategoria">Añade el nombre de la categoría a
										añadir:</label> <input type="text" name="newCatogria"
										placeholder="Escriba aquí la nueva categoría"
										class="form-control">
								</div>

							</form>
						</div>

						<div id="categoriasExistentes" class="row"></div>
					</div>

				</div>

				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
					<button type="button" class="btn btn-primary" onclick="addCat();">Añadir
						categoria</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->


	<!-- Bootstrap core JavaScript
    ================================================== 
	 Placed at the end of the document so the pages load faster-->
	
	<script type="text/javascript" src="../js/utils.js"></script>
	<script type="text/javascript" src="../js/jScript.js"></script>
	<script type="text/javascript" src="../js/validation.js"></script>
	<script type="text/javascript" src="../assets/js/jquery.js"></script>
	<script type="text/javascript" src="../js/bootstrap-datepicker.js"></script>
	<script type="text/javascript" src="../dist/js/bootstrap.js"></script>



	<script type="text/javascript">
	window.onload=initPage();

	
	</script>

</body>
</html>
